package com.hundsun.yr.universal.p2p.bean.enums;

import android.content.Context;

import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;
import com.hundsun.yr.universal.library.data.HSSharedPreferences;
import com.hundsun.yr.universal.p2p.HsApplication;
import com.hundsun.yr.universal.p2p.bean.ProtocolBean;
import com.hundsun.yr.universal.p2p.bean.enums.base.LabelEnum;
import com.hundsun.yr.universal.p2p.config.Config;

import java.util.List;

/**
 * 协议类型枚举类，里面封装了协议数据
 *
 * @author tonglj
 */
public class OssidEnum extends LabelEnum {

    /**
     *
     */
    private static final long serialVersionUID = 1L;

    private String lable;

    private int num;

    private HSSharedPreferences mConfigSP = null;

    /**
     * 注册协议
     */
    public static OssidEnum REGIST_PROTOCOL = new OssidEnum("REGIST_PROTOCOL", "平台用户服务协议", 1);

    public static OssidEnum INVESTMENT_PROTOCOL = new OssidEnum("UNDERGRADUATE", "借贷合同", 2);
    public static OssidEnum TRANSFER_PROTOCOL = new OssidEnum("TRANSFER_PROTOCOL", "转让协议", 3);
    public static OssidEnum RISK_DISCLOSURE = new OssidEnum("RISK_DISCLOSURE", "风险揭示书", 4);
    public static OssidEnum LEGAL_DECLARATION = new OssidEnum("LEGAL_DECLARATION", "法律声明", 5);
    public static OssidEnum PARTNER_PROTOCOL = new OssidEnum("PARTNER_PROTOCOL", "合伙人协议", 6);
    public static OssidEnum SERVICE_MANAGEMENT_PROTOCOL = new OssidEnum("SERVICE_MANAGEMENT_PROTOCOL", "业务员管理协议", 7);
    public static OssidEnum TRUSTEESHIP_PROTOCOL = new OssidEnum("TRUSTEESHIP_PROTOCOL", "银行账户存管三方协议", 8);
    public static OssidEnum OPEN_ACCTOUNT_PROTOCOL = new OssidEnum("OPEN_ACCTOUNT_PROTOCOL", "中网国投用户开户协议", 12);
    public static OssidEnum LOAN_SERVICE_PROTOCOL = new OssidEnum("LOAN_SERVICE_PROTOCOL", "借款协议 ", 9);
    public static OssidEnum LOAN_AUTHORIZATION_PROTOCOL = new OssidEnum("LOAN_AUTHORIZATION_PROTOCOL", "用户委托授权书", 10);
    public static OssidEnum TRUSTEESHIP_AUTHORIZATION_PROTOCOL = new OssidEnum("TRUSTEESHIP_AUTHORIZATION_PROTOCOL", "用户授权协议", 11);


    protected OssidEnum(String name, String lable, int num) {
        super(name, lable, "ossId");
        this.lable = lable;
        this.num = num;
    }

    public String getOssName() {
        return "《" + lable + "》";
    }

    public String getOssLabel() {
        return lable;
    }

    public void setOssName(String s) {
        this.lable = s;
    }

    /**
     * 返回协议名称(不含租户id)
     *
     * @return
     */
    public String getWordName() {
        return num + ".html";
    }

    public void modifyEnum(Context context) {
        if (HsApplication.protocolNameList == null || HsApplication.protocolNameList.size() == 0) {
            mConfigSP = new HSSharedPreferences(context, Config.SPPath.file_configInfo);
            Gson gson = new Gson();
            HsApplication.protocolNameList = gson.fromJson(
                    mConfigSP.getShareProf(ProtocolBean.PROTOCOL),
                    new TypeToken<List<ProtocolBean>>() {
                    }.getType());
        }

        if (HsApplication.protocolNameList != null) {
            for (int i = 0; i < HsApplication.protocolNameList.size(); i++) {
                getProtocolName(HsApplication.protocolNameList.get(i));
            }
        }
    }

    private void getProtocolName(ProtocolBean protocolBean) {

        if ("REGIST_PROTOCOL".equals(protocolBean.getType())) {
            REGIST_PROTOCOL.setOssName(protocolBean.getName());
        } else if ("UNDERGRADUATE".equals(protocolBean.getType())) {
            INVESTMENT_PROTOCOL.setOssName(protocolBean.getName());
        } else if ("TRANSFER_PROTOCOL".equals(protocolBean.getType())) {
            TRANSFER_PROTOCOL.setOssName(protocolBean.getName());
        } else if ("RISK_DISCLOSURE".equals(protocolBean.getType())) {
            RISK_DISCLOSURE.setOssName(protocolBean.getName());
        } else if ("LEGAL_DECLARATION".equals(protocolBean.getType())) {
            LEGAL_DECLARATION.setOssName(protocolBean.getName());
        } else if ("PARTNER_PROTOCOL".equals(protocolBean.getType())) {
            PARTNER_PROTOCOL.setOssName(protocolBean.getName());
        } else if ("SERVICE_MANAGEMENT_PROTOCOL".equals(protocolBean.getType())) {
            SERVICE_MANAGEMENT_PROTOCOL.setOssName(protocolBean.getName());
        } else if ("TRUSTEESHIP_PROTOCOL".equals(protocolBean.getType())) {
            TRUSTEESHIP_PROTOCOL.setOssName(protocolBean.getName());
        } else if ("LOAN_SERVICE_PROTOCOL".equals(protocolBean.getType())) {
            LOAN_SERVICE_PROTOCOL.setOssName(protocolBean.getName());
        } else if ("LOAN_AUTHORIZATION_PROTOCOL".equals(protocolBean.getType())) {
            LOAN_AUTHORIZATION_PROTOCOL.setOssName(protocolBean.getName());
        } else if ("TRUSTEESHIP_AUTHORIZATION_PROTOCOL".equals(protocolBean.getType())) {
            TRUSTEESHIP_AUTHORIZATION_PROTOCOL.setOssName(protocolBean.getName());
        }
    }

}
